// CalculatorServer.cc
#include "CalculatorServer.hpp"
#include <memory>
static int default_serverport = 8888; // 服务器的默认端口

// 业务处理函数
// req是经过反序列化后的
Response Calculate(const Request &req)
{
    // 先进行初始化
    Response res(0, 0);

    // 通过算式运算符选择具体的计算方式
    switch (req._op)
    {
    case '+':
        res._result = req._x + req._y;
        break;
    case '-':
        res._result = req._x - req._y;
        break;
    case '*':
        res._result = req._x * req._y;
        break;
    case '/':
        if (req._y == 0)
        {
            // 被除数不能为0
            res._code = 1;
        }
        else
        {
            res._result = req._x / req._y;
        }
        break;
    case '%':
        if (req._y == 0)
        {
            // 被除数不能为0
            res._code = 2;
        }
        else
        {
            res._result = req._x % req._y;
        }
        break;
    default:
        res._code = 3;
        break;
    }
    // 业务处理完后，返回回去
    return res;
}

int main()
{
    unique_ptr<CalculatorServer> server(new CalculatorServer(default_serverport, Calculate));
    // 初始化，进行通信前的基本创建和监听
    server->Init();
    server->Start();

    return 0;
}